package com.koushikdutta.ion;

import android.graphics.Bitmap;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import com.koushikdutta.async.future.SimpleFuture;
import com.koushikdutta.async.util.FileCache;
import com.koushikdutta.ion.bitmap.BitmapInfo;
import com.koushikdutta.ion.bitmap.PostProcess;
import com.koushikdutta.ion.bitmap.Transform;
import com.koushikdutta.ion.builder.Builders.Any.BF;
import java.util.ArrayList;
import java.util.Iterator;

abstract class IonBitmapRequestBuilder implements BF {
    static final /* synthetic */ boolean $assertionsDisabled = (!IonBitmapRequestBuilder.class.desiredAssertionStatus());
    private static final SimpleFuture<Bitmap> FUTURE_BITMAP_NULL_URI = new SimpleFuture<Bitmap>() {
        {
            setComplete(new NullPointerException("uri"));
        }
    };
    boolean animateGif = true;
    IonRequestBuilder builder;
    boolean deepZoom;
    boolean disableFadeIn;
    Ion ion;
    ArrayList<PostProcess> postProcess;
    int resizeHeight;
    int resizeWidth;
    ScaleMode scaleMode = ScaleMode.FitXY;
    ArrayList<Transform> transforms;

    void reset() {
        this.ion = null;
        this.transforms = null;
        this.scaleMode = ScaleMode.FitXY;
        this.resizeWidth = 0;
        this.resizeHeight = 0;
        this.disableFadeIn = false;
        this.animateGif = true;
        this.builder = null;
        this.deepZoom = false;
        this.postProcess = null;
    }

    public IonBitmapRequestBuilder(Ion ion) {
        this.ion = ion;
    }

    static void doAnimation(ImageView imageView, Animation animation, int animationResource) {
        if (imageView != null) {
            if (animation == null && animationResource != 0) {
                animation = AnimationUtils.loadAnimation(imageView.getContext(), animationResource);
            }
            if (animation == null) {
                imageView.setAnimation(null);
            } else {
                imageView.startAnimation(animation);
            }
        }
    }

    protected IonRequestBuilder ensureBuilder() {
        return this.builder;
    }

    private String computeDownloadKey() {
        String downloadKey = this.builder.uri;
        if (!this.animateGif) {
            downloadKey = downloadKey + ":!animateGif";
        }
        if (this.deepZoom) {
            downloadKey = downloadKey + ":deepZoom";
        }
        return FileCache.toKeyString(downloadKey);
    }

    protected void finalizeResize() {
    }

    public String computeBitmapKey(String downloadKey) {
        if ($assertionsDisabled || downloadKey != null) {
            if (this.resizeHeight > 0 || this.resizeWidth > 0 || this.scaleMode != ScaleMode.FitXY) {
                finalizeResize();
                if (this.resizeWidth >= 0 || this.resizeHeight >= 0) {
                    if (this.transforms == null) {
                        this.transforms = new ArrayList();
                    }
                    this.transforms.add(0, new DefaultTransform(this.resizeWidth, this.resizeHeight, this.scaleMode));
                } else {
                    throw new IllegalStateException("must provide valid resize dimensions if usingcenterCrop or centerInside\n\nValid values: (<=0, Y) or (X, <=0), or (X, Y), where:\nX and Y are greater than 0. Specifying <=0 values will scale proportionately.\nValues can also be implied through layout parameters.");
                }
            }
            String bitmapKey = downloadKey;
            if (!hasTransforms()) {
                return bitmapKey;
            }
            Iterator i$ = this.transforms.iterator();
            while (i$.hasNext()) {
                bitmapKey = bitmapKey + ((Transform) i$.next()).key();
            }
            return FileCache.toKeyString(bitmapKey);
        }
        throw new AssertionError();
    }

    BitmapFetcher executeCache() {
        String downloadKey = computeDownloadKey();
        String bitmapKey = computeBitmapKey(downloadKey);
        BitmapFetcher ret = new BitmapFetcher();
        ret.downloadKey = downloadKey;
        ret.bitmapKey = bitmapKey;
        ret.hasTransforms = hasTransforms();
        ret.resizeWidth = this.resizeWidth;
        ret.resizeHeight = this.resizeHeight;
        ret.builder = this.builder;
        ret.transforms = this.transforms;
        ret.animateGif = this.animateGif;
        ret.deepZoom = this.deepZoom;
        ret.postProcess = this.postProcess;
        if (!this.builder.noCache) {
            BitmapInfo bitmap = this.builder.ion.bitmapCache.get(bitmapKey);
            if (bitmap != null) {
                ret.info = bitmap;
            }
        }
        return ret;
    }

    boolean hasTransforms() {
        return this.transforms != null && this.transforms.size() > 0;
    }
}
